<script type="text/javascript">
var enable_submit = true;

function submit_snapshot_policy() {
    var unit = $("#snapshot_policy_create").find(".snanpshot_check:checked").val();
    var name = $("#snapshot_policy_create").find("#snanpshot_policy_name");
    var task_select = $("#snapshot_policy_create").find("#snapshot_day_select");
    if (unit == 'days') {
        task_select = $("#snapshot_policy_create").find("#snapshot_day_select");
    }
    if (unit == 'hours') {
        task_select = $("#snapshot_policy_create").find("#snapshot_hour_select");
    }
    var count = task_select.attr('realvalue');
    var bValid = true;
    bValid = bValid && checkRegexp(name, /^[\u4e00-\u9fa50-9a-z_-]+$/, "名称应由中文、小写英文字母、数字、中划线(英文)或下划线构成。");

    if (bValid) {
        if (enable_submit) {
            enable_submit = false;
            var $submitBtn = $('.modal-footer').find('.btn-blue');
            $submitBtn.html('<i class="fa fa-spinner fa-spin" style="margin:3px 6px;"></i>');
            $('.rtn-tip').slideUp('fast');
            $.post("/snapshot_policy_create", {
                    'name': $.trim(name.val()),
                    'unit': unit,
                    'count': count,
                },
                function(data) {
                    if (data.reply.is_success) {
                        angular.element('#auto_snapshot_policy').scope().close();
                        $("#snapshot_policy_table").trigger('reloadGrid');
                    } else {
                        error = data.reply.error;
                        $('.rtn-tip').text(error).slideDown('fast');
                        $submitBtn.html(SelfTranslate("[[ 'label ok'|translate ]]"));
                        enable_submit = true;
                    }
                });
        }
    }
}

$(function() {
    /* 为小图标绑定鼠标移入移出事件 */
    var obj = {
        "snanpshot_policy_name": "由中文、大小写英文字母、数字、中划线或下划线构成。"
    }
    bindMouseEvent('#snapshot_policy_create', obj);
    /* 为小图标绑定鼠标移入移出事件结束 */

    $(".snanpshot_check").click(function() {
        var method = $("#snapshot_policy_create").find(".snanpshot_check:checked").val();
        if (method == 'hours') {
            $("#snapshot_hour_select").show();
            $("#snapshot_day_select").hide();
            $("#selectDiv_snapshot_hour_select").show();
            $("#selectDiv_snapshot_day_select").hide();
        }
        if (method == 'days') {
            $("#snapshot_hour_select").hide();
            $("#snapshot_day_select").show();
            $("#selectDiv_snapshot_hour_select").hide();
            $("#selectDiv_snapshot_day_select").show();
        }
    });

    var days = []
    for (var i = 1; i < 31; i++) {
        days.push([i, i])
    }
    $('#snapshot_day_select').rqDropDown({
        position: true,
        "datatype": "local",
        "source": days
    });
    var hours = []
    for (var i = 1; i < 24; i++) {
        hours.push([i, i])
    }
    $('#snapshot_hour_select').rqDropDown({
        position: true,
        "datatype": "local",
        "source": hours
    });
    $("#selectDiv_snapshot_hour_select").hide();
    $("#selectDiv_snapshot_day_select").show();
});
</script>
<div class="modal-header">
    <h3>创建自动快照策略</h3>
</div>
<div class="modal-body modal-body-height" id="snapshot_policy_create">
    <div class="snapshot-policy-create-box">
        <div class="rtn-tip"></div>
        <table class="base-info">
            <tr>
                <td>
                    <label>名称</label><a>*</a>
                </td>
                <td colspan="3">
                    <input class="" id="snanpshot_policy_name" name="snanpshot_policy_name" size="25" type="text" />
                    <i class="fa fa-question-circle" itype="snanpshot_policy_name"></i>
                </td>
            </tr>
            <tr>
                <td>
                    <label>重复方式</label><a>*</a>
                </td>
                <td>
                    <label>
                        <input type="radio" class="snanpshot_check" name="snanpshot_check" value="days" style="vertical-align:-3px;" checked="true" />按天</label>
                    <label>
                        <input type="radio" class="snanpshot_check" name="snanpshot_check" value="hours" style="vertical-align:-3px;" />按小时</label>
                </td>
                <td>
                    <label>重复周期</label><a>*</a>
                </td>
                <td>
                    <input id="snapshot_day_select" class="rounded_dropdown" readonly="readonly" value="1" realvalue='1' />
                    <input id="snapshot_hour_select" class="rounded_dropdown" readonly="readonly"  style="display: none;" value="1" realvalue='1' />
                </td>
            </tr>
        </table>
    </div>
    <div class="error-tip">
        <p></p><span></span>
    </div>
    <div class="tip">
        <p></p><span></span>
    </div>
</div>
<div class="modal-footer">
    <button class="btn btn-blue" ng-click="snapshot_policy_create()">[[ 'label ok'|translate ]]</button>
    <button class="btn btn-white" ng-click="cancel()">[[ 'label cancel'|translate ]]</button>
</div>
